#include "MSqlConnection.h"

using namespace NsMSql;

QMap<QString, QSqlDatabase> MSqlConnection::m_defaultDbMap = QMap<QString, QSqlDatabase>();

MSqlConnection::MSqlConnection(const MSqlServerAttr& sqlServerAttr)
{
	m_sqlServerAttr = sqlServerAttr;

	//初始化数据库驱动
	switch (m_sqlServerAttr.type)
	{
	case eMySQL:
	{
		m_driverName = "QMYSQL";
	}break;
	case eSQLite:
	{
		m_driverName = "QSQLITE";
	}break;
	default:
		break;
	}
	//初始化全局默认连接
	if (!m_defaultDbMap.contains(m_driverName))
	{
		QSqlDatabase db = QSqlDatabase::addDatabase(m_driverName);
		m_defaultDbMap[m_driverName] = db;
	}
}

bool NsMSql::MSqlConnection::operator==(const MSqlConnection& other)
{
	if (m_db.connectionName() == other.m_db.connectionName())
		return true;
	return false;
}

bool NsMSql::MSqlConnection::openServer()
{
	QSqlDatabase& db = m_defaultDbMap[m_driverName];
	db.setHostName(m_sqlServerAttr.hostName);
	db.setPort(m_sqlServerAttr.port);
	db.setUserName(m_sqlServerAttr.userName);
	db.setPassword(m_sqlServerAttr.password);
	if (!db.open()) {
		MSqlError(QString("open host error, %1").arg(db.lastError().text()));
		return false;
	}

	return true;
}

bool NsMSql::MSqlConnection::openConnection(const QString& connectionName)
{
	if (!m_db.isOpen())
	{
		m_db = QSqlDatabase::addDatabase(m_driverName, connectionName);
		m_db.setHostName(m_sqlServerAttr.hostName);
		m_db.setPort(m_sqlServerAttr.port);
		m_db.setUserName(m_sqlServerAttr.userName);
		m_db.setPassword(m_sqlServerAttr.password);
		m_db.setDatabaseName(m_sqlServerAttr.databaseName);
		if (!m_db.open()) {
			MSqlError(QString("open datatabase error, %1").arg(m_db.lastError().text()));
			return false;
		}
		else
		{
			return true;
		}
	}

	return false;
}

void NsMSql::MSqlConnection::closeConnection()
{
	m_db.close();
}

void NsMSql::MSqlConnection::removeConnection()
{
	QSqlDatabase::removeDatabase(m_db.connectionName());
}

QString NsMSql::MSqlConnection::getConnectionName()
{
	QString res = "";
	if (m_db.isOpen())
		res = m_db.connectionName();
	return res;
}

bool NsMSql::MSqlConnection::execWithoutRes(const QString& sqlCmd, bool useDefaultConnection)
{
	QSqlDatabase& db = m_db;
	if (useDefaultConnection)
		db = m_defaultDbMap[m_driverName];
	if (db.isOpen())
	{
		QSqlQuery query(db);
		if (!query.exec(sqlCmd))
		{
			qDebug() << query.lastError();
			return false;
		}
		return true;
	}
	
	return false;
}

QSqlQuery NsMSql::MSqlConnection::execWithRes(const QString& sqlCmd, bool useDefaultConnection)
{
	QSqlDatabase& db = m_db;
	if (useDefaultConnection)
		db = m_defaultDbMap[m_driverName];
	if (db.isOpen())
	{
		QSqlQuery query(db);
		if (!query.exec(sqlCmd))
		{
			qDebug() << query.lastError();
			return QSqlQuery();
		}
		return query;
	}

	return QSqlQuery();
}

QString NsMSql::MSqlConnection::getDataTypeStr(MFieldDataType dataType)
{
	QString str;

	switch (dataType)
	{
	case NsMSql::eInt:
		str = "int";
		break;
	case NsMSql::eVarchar:
		str = "varchar";
		break;
	case NsMSql::eDouble:
		str = "double";
		break;
	case NsMSql::eDateTime:
		str = "datetime";
		break;
	default:
		break;
	}

	return str;
}

